package com.learn.dubbo.log.task;

import java.util.List;

import com.learn.dubbo.core.redis.core.JedisUtil;
import com.learn.dubbo.core.util.JsonUtil;
import com.learn.dubbo.entity.log.SysLog;
import com.learn.dubbo.log.ConstantObject;
import com.learn.dubbo.log.service.LogRemoteService;

/**
 * 日志保存线程，如果保存报错，把日志依然放回队列
 * @author Administrator
 *
 */
public class LogSaveThread implements Runnable {
	
	private LogRemoteService logRemoteService;

	public void setLogRemoteService(LogRemoteService logRemoteService) {
		this.logRemoteService = logRemoteService;
	}

	@Override
	public void run() {
		JedisUtil  jedisUtil = new JedisUtil();
		String logJson = null;
		while(true){
			try {
				List<String> list = jedisUtil.blpop(0,ConstantObject.LOG_SAVE_QUENE);
				logJson = list.get(1);
				SysLog sysLog = JsonUtil.readValue(logJson, SysLog.class);
				logRemoteService.saveSysLogToDB(sysLog);
				logJson="";
			} catch (Exception e) {
				if(logJson!=null&&logJson.length()>0){
					jedisUtil.lpush(ConstantObject.LOG_SAVE_QUENE, logJson);
				}
			}
		}
	}

}
